//
//  AppsEngine.m
//  HaoJieGPS
//
//  Created by Vescky on 13-6-18.
//  Copyright (c) 2013年 GDPU_DEV. All rights reserved.
//

#import "AppsEngine.h"
#import "JSON.h"

#define SERVER_GOLFFRIEND @"mingyuanhui.sinaapp.com/gfAPI"

@implementation AppsEngine

AppsEngine *appEngine;

- (NSDictionary *)toValue:(NSString *)json {
    NSString *jsonTmp = [json stringByReplacingOccurrencesOfString:@"null" withString:@""];
    
    if (![jsonTmp JSONValue]) {
        jsonTmp = [json stringByReplacingOccurrencesOfString:@"null" withString:@"\"\""];
    }
    
    NSDictionary *resultDict = [jsonTmp JSONValue];
    
    if (!resultDict) {
        resultDict = [json JSONValue];
    }
    
    if (!resultDict) {
        resultDict = [[NSDictionary alloc] init];
    }
    
    return resultDict;
}

+ (id)shareAppsEngine {
    if (!appEngine) {
        appEngine = [[self alloc] initWithHostName:SERVER_GOLFFRIEND customHeaderFields:nil];
    }
    return appEngine;
}

- (void)submitRequest:(NSString*)action param:(NSMutableDictionary*)paramsDic onCompletion:(JsonEngineBlock)completionBlock onError:(MKNKErrorBlock)errorBlock method:(NSString *)method {
    
    action = [action stringByAppendingString:@"/index.php"];
    NSMutableDictionary *argsDict = [[NSMutableDictionary alloc] initWithDictionary:paramsDic];
    [argsDict setObject:@"a41e3b00429a376cc13c2dc33370eef9" forKey:@"gfID"];
    
    MKNetworkOperation *op = [self operationWithPath:action params:argsDict httpMethod:method];
    
    [op onCompletion:^(MKNetworkOperation *completedOperation) {
        
        NSString *responseString = [completedOperation responseString];
        
        completionBlock([self toValue:responseString]);
        
    } onError:^(NSError *error) {
        
        errorBlock(error);
    }];
    
    [self emptyCache];
    [self enqueueOperation:op];
}

- (void)submitSimpleRequest:(NSString*)url onCompletion:(JsonEngineBlock)completionBlock onError:(MKNKErrorBlock)errorBlock method:(NSString *)method {
    
    MKNetworkOperation *op = [self operationWithURLString:url params:nil httpMethod:method];
    
//    MKNetworkOperation *op = [self operationWithPath:url params:nil httpMethod:method];
    [op onCompletion:^(MKNetworkOperation *completedOperation) {
        
        NSString *responseString = [completedOperation responseString];
        
        completionBlock([self toValue:responseString]);
        
    } onError:^(NSError *error) {
        
        errorBlock(error);
    }];
    
    [self emptyCache];
    [self enqueueOperation:op];
}

//- (void)getVersionOnCompletion:(JsonEngineBlock)completionBlock onError:(MKNKErrorBlock)errorBlock method:(NSString *)method {
//    
//    MKNetworkOperation *op = [self operationWithPath:require params:nil httpMethod:method];
//    
//    [op onCompletion:^(MKNetworkOperation *completedOperation) {
//        
//        NSString *responseString = [completedOperation responseString];
//        
//        completionBlock([self toValue:responseString]);
//        
//    } onError:^(NSError *error) {
//        
//        errorBlock(error);
//    }];
//    
//    [self emptyCache];
//    [self enqueueOperation:op];
//}

@end
